/*
 Main Figures 2, 3
 Main Tables 1, 2, 4, 5, 6 (Tables 3 and 7 are made in scripts 03 and 04)
*/


local PATH_FIG = "~/Downloads/figures"
local PATH_TBL = "~/Downloads/tables"
local PATH_NUM = "~/Downloads/numbers"

set graphics off // avoid displaying the graphs (instead just save them)
set linesize 250 // to keep table outputs wide


/* Make Figures and store numbers for Tables */


  /* STATE PARTISAN RACES */

  use wide_state_partisan_races, clear
  
  gen D_pct = 100 * D_choice/(D_choice + R_choice)
  gen competitive = (D_pct > 5 & D_pct < 95)
  
  // APG and Table 1
  gen D_pct_D_pid = 100 * D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen D_pct_R_pid = 100 * D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)
  gen diff_DR       =    (D_pct_D_pid - D_pct_R_pid)
  gen apg_DR        = abs(D_pct_D_pid - D_pct_R_pid)
  
  sum D_pct_D_pid if competitive == 1
   local D_spo_Dpid = r(mean)
  sum D_pct_R_pid if competitive == 1
   local D_spo_Rpid = r(mean)
  sum diff_DR     if competitive == 1
   local D_spo_DR   = r(mean)
  sum apg_DR      if competitive == 1
   local AD_spo_DR  = r(mean)
   local N_spo      = r(N)

  // log odds version of Table 1
  gen D_share_D_pid = D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen D_share_R_pid = D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)
  gen alo_DR        = (D_share_D_pid/(1 - D_share_D_pid))/(D_share_R_pid/(1 - D_share_R_pid))
  replace alo_DR = 1/alo_DR if alo_DR < 1
  replace alo_DR = log(alo_DR)
  sum alo_DR if competitive == 1
   local ADL_spo_DR  = r(mean)
  
  // Table 2 and Appendix Figure on rolloff
  gen U_pct = 100 * U_choice/N if U_choice != 0
  sum U_pct if competitive == 0
   local U_spo_u   = r(mean)
   local N_spo_u   = r(N)
  sum U_pct if competitive == 1
   local U_spo_c   = r(mean)
   local N_spo_c   = r(N)

  replace X_choice = 0 if X_choice == .
  gen X_pct = 100 * X_choice/N
  sum X_pct if competitive == 0
   local X_spo_u   = r(mean)
  sum X_pct if competitive == 1
   local X_spo_c   = r(mean)

  twoway (scatter U_R U_D if (D_in_race >= 1 & R_in_race >= 1), msize(.5) mcolor(gs0)) ///
         (scatter U_R U_D if (D_in_race == 0 | R_in_race == 0), msize(.5) mcolor(gs8)), ///
          xlabel(0 20 40 60 80, labsize(vlarge)) ylabel(0 20 40 60 80, labsize(vlarge)) /// 
		  xscale(range(0 93)) yscale(range(0 93)) legend(off) aspectratio(1) ///
          xtitle("Roll-off for Democrats", size(huge)) ytitle("Roll-off for Repubs", size(huge)) ///
		  title("State Partisan Offices", size(vhuge))
  graph export "`PATH_FIG'/rolloff_state_partisan_offices.pdf", replace 

  keep if competitive == 1

  * Figure 2
  twoway (histogram alo_DR, bin(50) fraction xtitle("Absolute Log Odds Ratio", size(huge)) ///
    ytitle("") lcolor(gs0) fcolor(gs8)) , title("State Partisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 2 4 6 8 10, labsize(vlarge)) ylabel(0 .05 .10, labsize(vlarge))
  graph export "`PATH_FIG'/hist_state_partisan_offices_lo.pdf", replace

  twoway (histogram apg_DR, bin(50) fraction xtitle("Absolute Partisan Gap", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)) , title("State Partisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 20 40 60 80 100, labsize(vlarge)) ylabel(0 .05 .10 .15, labsize(vlarge))
  graph export "`PATH_FIG'/fg2_hist_state_partisan_offices.pdf", replace


  /* LOCAL PARTISAN RACES */
  
  use wide_local_partisan_races, clear
  
  gen D_pct = 100 * D_choice/(D_choice + R_choice)
  gen competitive = (D_pct > 5 & D_pct < 95)
  
  // APG and Table 1
  gen D_pct_D_pid = 100 * D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen D_pct_R_pid = 100 * D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)
  gen diff_DR       =    (D_pct_D_pid - D_pct_R_pid)
  gen apg_DR        = abs(D_pct_D_pid - D_pct_R_pid)
  
  sum D_pct_D_pid if competitive == 1
   local D_lpo_Dpid = r(mean)
  sum D_pct_R_pid if competitive == 1
   local D_lpo_Rpid = r(mean)
  sum apg_DR      if competitive == 1
   local AD_lpo_DR  = r(mean)
   local N_lpo      = r(N)

  // log odds version of Table 1
  gen D_share_D_pid = D_choice_D_pid/(D_choice_D_pid + R_choice_D_pid)
  gen D_share_R_pid = D_choice_R_pid/(D_choice_R_pid + R_choice_R_pid)
  gen alo_DR        = (D_share_D_pid/(1 - D_share_D_pid))/(D_share_R_pid/(1 - D_share_R_pid))
  replace alo_DR = 1/alo_DR if alo_DR < 1
  replace alo_DR = log(alo_DR)
  sum alo_DR if competitive == 1
   local ADL_lpo_DR  = r(mean)

  
  // Table 2 and Appendix Figure on rolloff
  gen U_pct = 100 * U_choice/N if U_choice != 0
  sum U_pct if competitive == 0
   local U_lpo_u   = r(mean)
   local N_lpo_u   = r(N)
  sum U_pct if competitive == 1
   local U_lpo_c   = r(mean)
   local N_lpo_c   = r(N)

  replace X_choice = 0 if X_choice == .
  gen X_pct = 100 * X_choice/N
  sum X_pct if competitive == 0
   local X_lpo_u   = r(mean)
  sum X_pct if competitive == 1
   local X_lpo_c   = r(mean)

  twoway (scatter U_R U_D if (D_in_race >= 1 & R_in_race >= 1), msize(.5) mcolor(gs0)) ///
         (scatter U_R U_D if (D_in_race == 0 | R_in_race == 0), msize(.5) mcolor(gs8)), /// 
          xlabel(0 20 40 60 80, labsize(vlarge)) ylabel(0 20 40 60 80, labsize(vlarge)) ///
		  xscale(range(0 93)) yscale(range(0 93)) legend(off) aspectratio(1) ///
          xtitle("Roll-off for Democrats", size(huge)) ytitle("Roll-off for Repubs", size(huge)) ///
		  title("Local Partisan Offices", size(vhuge)) 
  graph export "`PATH_FIG'/rolloff_local_partisan_offices.pdf", replace

  keep if competitive == 1

  // Figure 2
  twoway (histogram alo_DR, bin(50) fraction xtitle("Absolute Log Odds Ratio", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)), title("Local Partisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 2 4 6 8 10, labsize(vlarge)) ylabel(0 .05 .10, labsize(vlarge)) 
  graph export "`PATH_FIG'/hist_local_partisan_offices_lo.pdf", replace

  twoway (histogram apg_DR, bin(50) fraction xtitle("Absolute Partisan Gap", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)), title("Local Partisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 20 40 60 80 100, labsize(vlarge)) ylabel(0 .05 .10 .15, labsize(vlarge)) 
  graph export "`PATH_FIG'/fg2_hist_local_partisan_offices.pdf", replace


  // Table 4, APG by different offices
  gen     item_type = "CO_LEG"  if strpos(item, "CO_COMM") > 0 | strpos(item, "CO_SUPERV") > 0 | strpos(item, "FREEHOLDER") > 0
  replace item_type = "SHERIFF" if strpos(item, "SHERIFF") > 0
  replace item_type = "CO_OTH"  if strpos(item, "CO_") > 0 & item_type == ""
  replace item_type = "MAYOR"   if strpos(item, "MAYOR") > 0   | strpos(item, "TWP SUPERVISOR") > 0
  replace item_type = "MUN_LEG" if strpos(item, "COUNCIL") > 0 | strpos(item, "TWP COMM")  > 0 | strpos(item, "TWP TRUSTEE") > 0

  sum D_pct_D_pid if item_type == "CO_LEG"
   local D_CO_LEG_Dpid  = r(mean)
  sum D_pct_R_pid if item_type == "CO_LEG"
   local D_CO_LEG_Rpid  = r(mean)
  sum diff_DR       if item_type == "CO_LEG"
   local D_CO_LEG_DR    = r(mean)
  sum apg_DR    if item_type == "CO_LEG"
   local AD_CO_LEG_DR   = r(mean)
   local N_CO_LEG       = r(N)
  sum D_pct_D_pid if item_type == "SHERIFF"
   local D_SHERIFF_Dpid = r(mean)
  sum D_pct_R_pid if item_type == "SHERIFF"
   local D_SHERIFF_Rpid = r(mean)
  sum diff_DR       if item_type == "SHERIFF"
   local D_SHERIFF_DR   = r(mean)
  sum apg_DR    if item_type == "SHERIFF"
   local AD_SHERIFF_DR  = r(mean)
   local N_SHERIFF      = r(N)
  sum D_pct_D_pid if item_type == "CO_OTH"
   local D_CO_OTH_Dpid  = r(mean)
  sum D_pct_R_pid if item_type == "CO_OTH"
   local D_CO_OTH_Rpid  = r(mean)
  sum diff_DR       if item_type == "CO_OTH"
   local D_CO_OTH_DR    = r(mean)
  sum apg_DR    if item_type == "CO_OTH"
   local AD_CO_OTH_DR   = r(mean)
   local N_CO_OTH       = r(N)
  sum D_pct_D_pid if item_type == "MAYOR"
   local D_MAYOR_Dpid   = r(mean)
  sum D_pct_R_pid if item_type == "MAYOR"
   local D_MAYOR_Rpid   = r(mean)
  sum diff_DR       if item_type == "MAYOR"
   local D_MAYOR_DR     = r(mean)
  sum apg_DR    if item_type == "MAYOR"
   local AD_MAYOR_DR    = r(mean)
   local N_MAYOR        = r(N)
  sum D_pct_D_pid if item_type == "MUN_LEG"
   local D_MUN_LEG_Dpid = r(mean)
  sum D_pct_R_pid if item_type == "MUN_LEG"
   local D_MUN_LEG_Rpid = r(mean)
  sum diff_DR       if item_type == "MUN_LEG"
   local D_MUN_LEG_DR   = r(mean)
  sum apg_DR    if item_type == "MUN_LEG"
   local AD_MUN_LEG_DR  = r(mean)
   local N_MUN_LEG      = r(N)

  // Appendix C: by size
  gen logN = log(N)
  reg apg_DR logN
  local r2_lpo = e(r2)

  lowess apg_DR logN, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
  title("Local Partisan Offices", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Log Total Voters", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xlabel(5 10 15, labsize(vlarge)) ylabel(0 20 40 60 80 100, labsize(vlarge)) 
  graph export "`PATH_FIG'/local_partisan_offices_vs_size.pdf", replace


  /* STATE NONPARTISAN RACES */

  use wide_state_nonpartisan_races, clear
  
  gen A_pct = 100 * A_choice/(A_choice + B_choice)
  gen competitive = (A_pct > 5 & A_pct < 95)
  
  // APG and Table 1
  gen A_pct_D_pid = 100 * A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen A_pct_R_pid = 100 * A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)
  gen apg_DR        = abs(A_pct_D_pid - A_pct_R_pid)
  
  sum apg_DR if competitive == 1
   local AA_sno_DR = r(mean)
   local N_sno     = r(N)

  gen A_share_D_pid = A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen A_share_R_pid = A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)
  gen alo_DR        = (A_share_D_pid/(1 - A_share_D_pid))/(A_share_R_pid/(1 - A_share_R_pid))
  replace alo_DR = 1/alo_DR if alo_DR < 1
  replace alo_DR = log(alo_DR)
  sum alo_DR if competitive == 1
   local AAL_sno_DR  = r(mean)
  
  // Table 2 and Appendix figures on rolloff
  gen U_pct = 100 * U_choice/N if U_choice != 0
  corr U_P U_S if competitive == 1
   local U_sno_comp_PScorr = r(rho)

  twoway (scatter U_R U_D if (competitive == 1), msize(.5) mcolor(gs0)) ///
         (scatter U_R U_D if (competitive == 0), msize(.5) mcolor(gs8)),  ///
          xlabel(0 20 40 60 80, labsize(vlarge)) ylabel(0 20 40 60 80, labsize(vlarge)) ///
		  xscale(range(0 93)) yscale(range(0 93)) legend(off) aspectratio(1) ///
          xtitle("Roll-off for Democrats", size(huge)) ytitle("Roll-off for Repubs", size(huge)) ///
		  title("State Nonpartisan Offices", size(vhuge))
  graph export "`PATH_FIG'/rolloff_state_nonpartisan_offices.pdf", replace

  sum U_pct if competitive == 0
   local U_sno_u   = r(mean)
   local N_sno_u   = r(N)
  sum U_pct if competitive == 1
   local U_sno_c   = r(mean)
   local N_sno_c   = r(N)

  replace X_choice = 0 if X_choice == .
  gen X_pct = 100 * X_choice/N
  sum X_pct if competitive == 0
   local X_sno_u   = r(mean)
  sum X_pct if competitive == 1
   local X_sno_c   = r(mean)

  keep if competitive == 1

  * separating Ohio de facto partisan judges aside
  sum apg_DR if !(state == "OH" & (strpos(item, "JUDGE") > 0 | strpos(item, "JUSTICE") > 0))
  local mean_AA_sno_x = r(mean)
  count if apg_DR != . & apg_DR > 40
  local sum_sno_over40 = r(N)
  count if apg_DR != .
  local sum_sno        = r(N)
  count if apg_DR != . & apg_DR > 40 & !(state == "OH" & (strpos(item, "JUDGE") > 0 | strpos(item, "JUSTICE") > 0))
  local sum_sno_over40_x = r(N)
  count if apg_DR != .               & !(state == "OH" & (strpos(item, "JUDGE") > 0 | strpos(item, "JUSTICE") > 0))
  local sum_sno_x = r(N)

  * Figure 2
  twoway (histogram alo_DR, bin(50) fraction xtitle("Absolute Log Odds Ratio", size(huge)) ///
   ytitle("") lcolor(gs0) fcolor(gs8)) , title("State Nonpartisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 2 4 6 8 10, labsize(vlarge)) ylabel(0 .05 .10, labsize(vlarge)) 
  graph export "`PATH_FIG'/hist_state_nonpartisan_offices_lo.pdf", replace

  twoway (histogram apg_DR, bin(50) fraction xtitle("Absolute Partisan Gap", size(huge)) ///
   ytitle("") lcolor(gs0) fcolor(gs8)) , title("State Nonpartisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 20 40 60 80 100, labsize(vlarge)) ylabel(0 .05 .10 .15, labsize(vlarge))
  graph export "`PATH_FIG'/fg2_hist_state_nonpartisan_offices.pdf", replace


  /* LOCAL NONPARTISAN RACES */

  use wide_local_nonpartisan_races, clear
  
  gen A_pct = 100 * A_choice/(A_choice + B_choice)
  gen competitive = (A_pct > 5 & A_pct < 95)
  
  gen OTH_choice = N - U_choice - A_choice - B_choice
  gen pct_AB = (A_choice + B_choice)/(A_choice + B_choice + OTH_choice)
  drop if pct_AB < .75
  drop OTH_choice

  // APG and Table 1
  gen A_pct_D_pid = 100 * A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen A_pct_R_pid = 100 * A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)
  gen apg_DR        = abs(A_pct_D_pid - A_pct_R_pid)
  
  sum apg_DR if competitive == 1
   local AA_lno_DR = r(mean)
   local N_lno     = r(N)
  gen A_share_D_pid = A_choice_D_pid/(A_choice_D_pid + B_choice_D_pid)
  gen A_share_R_pid = A_choice_R_pid/(A_choice_R_pid + B_choice_R_pid)
  gen alo_DR        = (A_share_D_pid/(1 - A_share_D_pid))/(A_share_R_pid/(1 - A_share_R_pid))
  replace alo_DR = 1/alo_DR if alo_DR < 1
  replace alo_DR = log(alo_DR)
  sum alo_DR if competitive == 1
   local AAL_lno_DR  = r(mean)
   

  // Table 2 and Appendix figures on rolloff
  gen U_pct = 100 * U_choice/N if U_choice != 0
  sum U_pct if competitive == 0
   local U_lno_u   = r(mean)
   local N_lno_u   = r(N)
  sum U_pct if competitive == 1
   local U_lno_c   = r(mean)
   local N_lno_c   = r(N)

  replace X_choice = 0 if X_choice == .
  gen X_pct = 100 * X_choice/N
  sum X_pct if competitive == 0
   local X_lno_u   = r(mean)
  sum X_pct if competitive == 1
   local X_lno_c   = r(mean)

  replace U_D = . if N_D < 20 | N_R < 20
  replace U_R = . if N_D < 20 | N_R < 20
  replace U_S = . if N_D < 20 | N_R < 20
  replace U_S = . if U_D == .
  replace U_P = . if N_D < 20 | N_R < 20

  * Fg C5: rolloff
  corr U_P U_S if competitive == 1
   local U_lno_comp_PScorr = r(rho)

  twoway (scatter U_R U_D if (competitive == 1), msize(.5) mcolor(gs0)) ///
         (scatter U_R U_D if (competitive == 0), msize(.5) mcolor(gs8)), /// 
          xlabel(0 20 40 60 80, labsize(vlarge)) ylabel(0 20 40 60 80, labsize(vlarge)) ///
		  xscale(range(0 93)) yscale(range(0 93)) legend(off) aspectratio(1) ///
          xtitle("Roll-off for Democrats", size(huge)) ytitle("Roll-off for Repubs", size(huge)) ///
		  title("Local Nonpartisan Offices", size(vhuge))

  graph export "`PATH_FIG'/rolloff_local_nonpartisan_offices.pdf", replace

  twoway (scatter U_D U_S if (competitive == 1 & N_S >= 30), msize(.2) mcolor(gs0)) ///
         (scatter U_R U_S if (competitive == 1 & N_S >= 30), msize(.2) mcolor(gs8)), ///
          xlabel(0 10 20 30 40, labsize(vlarge)) ylabel(0 10 20 30 40, labsize(vlarge)) ///
		  xscale(range(0 40)) yscale(range(0 53)) legend(off)  aspectratio(1) ///
          xtitle("Roll-off for Democrats", size(huge)) ytitle("Roll-off for Repubs", size(huge)) ///
		  title("Local Nonpartisan Offices", size(vhuge)) 
  graph export "`PATH_FIG'/rolloff_local_nonpartisan_offices_2.pdf", replace


  keep if competitive == 1

  // Figure 2
  twoway (histogram alo_DR, bin(50) fraction xtitle("Absolute Log Odds Ratio", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)), title("Local Nonpartisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 2 4 6 8 10, labsize(vlarge)) ylabel(0 .05 .10, labsize(vlarge))
  graph export "`PATH_FIG'/hist_local_nonpartisan_offices_lo.pdf", replace

  twoway (histogram apg_DR, bin(50) fraction xtitle("Absolute Partisan Gap", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)), title("Local Nonpartisan Offices", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 20 40 60 80 100, labsize(vlarge)) ylabel(0 .05 .10 .15, labsize(vlarge))
  graph export "`PATH_FIG'/fg2_hist_local_nonpartisan_offices.pdf", replace
 
  count if apg_DR != . & apg_DR > 40
  local sum_lno_over40 = r(N)
  count if apg_DR != .
  local sum_lno        = r(N)
  

  // Table 3: Types of candidate local offices
  gen     item_type = "CO_LEG"  if strpos(item, "CO_COMM") > 0 | strpos(item, "CO_SUPERV") > 0 | strpos(item, "FREEHOLDER") > 0
  replace item_type = "SHERIFF" if strpos(item, "SHERIFF") > 0
  replace item_type = "CO_OTH"  if strpos(item, "CO_") > 0 & item_type == ""
  replace item_type = "MAYOR"   if strpos(item, "MAYOR") > 0   | strpos(item, "TWP SUPERVISOR") > 0
  replace item_type = "MUN_LEG" if strpos(item, "COUNCIL") > 0 | strpos(item, "TWP COMM")  > 0 | strpos(item, "TWP TRUSTEE") > 0
  replace item_type = "LOC_BOE" if item == "CO_BOE" | substr(item, -4, 4) == " BOE" | strpos(item, " BOE ") > 0 | strpos(item, "SCHOOL BOARD") > 0 | strpos(item, "SCHOOL GOV BOARD") > 0 | ///
    strpos(item, "SCHOOL DIST") > 0 | strpos(item, "SCHOOL TRUSTEE") > 0 | strpos(item, "SCHOOL SYSTEM") > 0 | strpos(item, "SCHOOL COMM") > 0 | strpos(item, "UNIFIED") > 0 | strpos(item, "EDUC") > 0

  sum apg_DR    if item_type == "CO_LEG"
   local AA_CO_LEG_DR  = r(mean)
   local N_CO_LEG_n    = r(N)
  sum apg_DR    if item_type == "SHERIFF"
   local AA_SHERIFF_DR = r(mean)
   local N_SHERIFF_n   = r(N)
  sum apg_DR    if item_type == "CO_OTH"
   local AA_CO_OTH_DR  = r(mean)
   local N_CO_OTH_n    = r(N)
  sum apg_DR    if item_type == "MAYOR"
   local AA_MAYOR_DR   = r(mean)
   local N_MAYOR_n     = r(N)
  sum apg_DR    if item_type == "MUN_LEG"
   local AA_MUN_LEG_DR = r(mean)
   local N_MUN_LEG_n   = r(N)
  sum apg_DR    if item_type == "LOC_BOE"
   local AA_LOC_BOE_DR = r(mean)
   local N_LOC_BOE_n   = r(N)


   // Fig 3
   gen apg_DR_compet = apg_DR if A_pct < 66.666667
   gen local_area_D_pct = 100 * (A_choice_D_pid + B_choice_D_pid)/(A_choice_D_pid + B_choice_D_pid + A_choice_R_pid + B_choice_R_pid)


  lowess apg_DR_compet local_area_D_pct, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
    title("Competitive Local Nonpartisan Offices", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Democratic Percent in Local Area", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) /// 
	xline(50) note("") ///
	xlabel(0 25 50 75 100, labsize(large)) ylabel(0 20 40 60 80 100, labsize(large)) 
  graph export "`PATH_FIG'/fg3_apg_vs_local_area_dem_pct_compet.pdf", replace

  lowess apg_DR local_area_D_pct, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
    title("All Local Nonpartisan Offices", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Democratic Percent in Local Area", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xline(50) note("") ///
	xlabel(0 25 50 75 100, labsize(large)) ylabel(0 20 40 60 80 100, labsize(large))
  graph export "`PATH_FIG'/fg3_apg_vs_local_area_dem_pct.pdf", replace

  
  // Appendix C: by size, competitiveness etc
  gen logN = log(N)
  reg apg_DR logN
   local r2_npo = e(r2)

  lowess apg_DR logN, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
  title("Local Nonpartisan Offices", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Log Total Voters", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xlabel(5 10 15, labsize(large)) ylabel(0 20 40 60 80 100, labsize(large))
  graph export "`PATH_FIG'/local_nonpartisan_offices_vs_size.pdf", replace

    gen margin = abs(A_pct - 50)
  lowess apg_DR margin, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
  title("Local Nonpartisan Offices", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Margin of Victory", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xlabel(0 10 20 30 40 50, labsize(large)) ylabel(0 20 40 60 80 100, labsize(large))
  graph export "`PATH_FIG'/local_nonpartisan_offices_vs_margin.pdf", replace


  /* STATE BALLOT MEASURES */

  use wide_state_ballot_measures, clear
  
  gen Y_pct = 100 * Y_choice/(Y_choice + N_choice)
  gen competitive = (Y_pct > 5 & Y_pct < 95)
  
  // APG and Table 1
  keep if competitive == 1 // doesn't drop anything only for state ballot measure, so ok to keep at top unlike the other offices
  gen Y_pct_D_pid = 100 * Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_pct_R_pid = 100 * Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen Y_diff_DR     =    (Y_pct_D_pid - Y_pct_R_pid)
  gen Y_apg_DR      = abs(Y_pct_D_pid - Y_pct_R_pid)

  sum Y_apg_DR
   local AY_sbm_DR = r(mean)
   local N_sbm     = r(N)
  gen Y_share_D_pid = Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_share_R_pid = Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen Y_alo_DR        = (Y_share_D_pid/(1 - Y_share_D_pid))/(Y_share_R_pid/(1 - Y_share_R_pid))
  replace Y_alo_DR = 1/Y_alo_DR if Y_alo_DR < 1
  replace Y_alo_DR = log(Y_alo_DR)
  sum Y_alo_DR
   local AYL_sbm_DR  = r(mean)

  count if Y_apg_DR != . & Y_apg_DR > 40
  local sum_sbm_over40 = r(N)
  count if Y_apg_DR != .
  local sum_sbm        = r(N)
  
  // Table 2: rolloff
  gen U_pct = 100 * U_choice/N if U_choice != 0
  sum U_pct if competitive == 1
   local U_sbm_c   = r(mean)
   local N_sbm_c   = r(N)

  corr U_P U_S if competitive == 1
   local U_sbm_comp_PScorr = r(rho)

  twoway (scatter U_R U_D if (competitive == 1), msize(.2) mcolor(gs0)) ///
         (scatter U_R U_D if (competitive == 0), msize(.2) mcolor(gs8)), /// 
          xlabel(0 20 40 60 80, labsize(vlarge)) ylabel(0 20 40 60 80, labsize(vlarge)) ///
		  xscale(range(0 93)) yscale(range(0 93)) legend(off) aspectratio(1) ///
          xtitle("Roll-off for Democrats", size(huge)) ytitle("Roll-off for Repubs", size(huge)) title("State Ballot Measures", size(vhuge))
  graph export "`PATH_FIG'/rolloff_state_ballot_measures.pdf", replace

  
  // Figure 2  
  twoway (histogram Y_alo_DR, bin(50) fraction xtitle("Absolute Log Odds Ratio", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)), title("State Ballot Measures", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) xlabel(0 2 4 6 8 10, labsize(vlarge)) ylabel(0 .05 .10, labsize(vlarge))
  graph export "`PATH_FIG'/hist_state_ballot_measures_lo.pdf", replace

  twoway (histogram Y_apg_DR, bin(50) fraction xtitle("Absolute Partisan Gap", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)) , title("State Ballot Measures", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 20 40 60 80 100, labsize(vlarge)) ylabel(0 .05 .10 .15, labsize(vlarge))
  graph export "`PATH_FIG'/fg2_hist_state_ballot_measures.pdf", replace


  // Table 5
  gen     L_pct_D_pid =       Y_pct_D_pid if yes_is_lib == 1
  replace L_pct_D_pid = 100 - Y_pct_D_pid if yes_is_lib == 0
  gen     L_pct_R_pid =       Y_pct_R_pid if yes_is_lib == 1
  replace L_pct_R_pid = 100 - Y_pct_R_pid if yes_is_lib == 0
  gen     L_diff_DR    =    (L_pct_D_pid - L_pct_R_pid) if yes_is_lib != .
  gen     L_apg_DR = abs(L_pct_D_pid - L_pct_R_pid) if yes_is_lib != .
  sum L_pct_D_pid if yes_is_lib != . & spending != 1 // liberal by our definition excludes spending
   local L_isbm_Dpid = r(mean)
  sum L_pct_R_pid if yes_is_lib != . & spending != 1
   local L_isbm_Rpid = r(mean)
  sum L_diff_DR     if yes_is_lib != . & spending != 1
   local L_isbm_DR   = r(mean)
  sum L_apg_DR  if yes_is_lib != . & spending != 1
   local AL_isbm_DR  = r(mean)
   local N_isbm      = r(N)
  sum L_pct_D_pid if yes_is_lib != . & spending == 1
   local L_tsbm_Dpid = r(mean)
  sum L_pct_R_pid if yes_is_lib != . & spending == 1
   local L_tsbm_Rpid = r(mean)
  sum L_diff_DR     if yes_is_lib != . & spending == 1
   local L_tsbm_DR   = r(mean)
  sum L_apg_DR  if yes_is_lib != . & spending == 1
   local AL_tsbm_DR  = r(mean)
   local N_tsbm      = r(N)


  /* LOCAL BALLOT MEASURES */

  use wide_local_ballot_measures, clear
  
  gen Y_pct = 100 * Y_choice/(Y_choice + N_choice)
  gen competitive = (Y_pct > 5 & Y_pct < 95)
  
  // APG and Table 1
  gen Y_pct_D_pid = 100 * Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_pct_R_pid = 100 * Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen Y_diff_DR     = (Y_pct_D_pid - Y_pct_R_pid)
  gen Y_apg_DR      = abs(Y_pct_D_pid - Y_pct_R_pid)
  
  sum Y_apg_DR
   local AY_lbm_DR  = r(mean)
   local N_lbm      = r(N)

  gen Y_share_D_pid = Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_share_R_pid = Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen Y_alo_DR        = (Y_share_D_pid/(1 - Y_share_D_pid))/(Y_share_R_pid/(1 - Y_share_R_pid))
  replace Y_alo_DR = 1/Y_alo_DR if Y_alo_DR < 1
  replace Y_alo_DR = log(Y_alo_DR)
  sum Y_alo_DR
   local AYL_lbm_DR  = r(mean)

  count if Y_apg_DR != . & Y_apg_DR > 40
  local sum_lbm_over40 = r(N)
  count if Y_apg_DR != .
  local sum_lbm        = r(N)

  // Table 2 and rolloff
  gen U_pct = 100 * U_choice/N if U_choice != 0
  sum U_pct if competitive == 1
   local U_lbm_c   = r(mean)
   local N_lbm_c   = r(N)
   
  replace U_D = . if N_D < 20 | N_R < 20
  replace U_R = . if N_D < 20 | N_R < 20
  replace U_S = . if N_D < 20 | N_R < 20

  corr U_P U_S if competitive == 1
   local U_lbm_comp_PScorr = r(rho)

  twoway (scatter U_R U_D if (competitive == 1), msize(.2) mcolor(gs0)) ///
         (scatter U_R U_D if (competitive == 0), msize(.2) mcolor(gs8)), /// 
          xlabel(0 20 40 60 80, labsize(vlarge)) ylabel(0 20 40 60 80, labsize(vlarge)) ///
		  xscale(range(0 93)) yscale(range(0 93)) legend(off) aspectratio(1) ///
          xtitle("Roll-off for Democrats", size(huge)) ytitle("Roll-off for Repubs", size(huge)) ///
		  title("Local Ballot Measures", size(vhuge))
  graph export "`PATH_FIG'/rolloff_local_ballot_measures.pdf", replace

  keep if competitive == 1

  
  // Tb 5: Ballot Measures by Type
  gen     L_pct_D_pid =       Y_pct_D_pid if yes_is_lib == 1
  replace L_pct_D_pid = 100 - Y_pct_D_pid if yes_is_lib == 0
  gen     L_pct_R_pid =       Y_pct_R_pid if yes_is_lib == 1
  replace L_pct_R_pid = 100 - Y_pct_R_pid if yes_is_lib == 0
  gen     L_diff_DR    =    (L_pct_D_pid - L_pct_R_pid) if yes_is_lib != .
  gen     L_apg_DR = abs(L_pct_D_pid - L_pct_R_pid) if yes_is_lib != .
  sum L_pct_D_pid if yes_is_lib != . & spending != 1
   local L_ilbm_Dpid = r(mean)
  sum L_pct_R_pid if yes_is_lib != . & spending != 1
   local L_ilbm_Rpid = r(mean)
  sum L_diff_DR     if yes_is_lib != . & spending != 1
   local L_ilbm_DR   = r(mean)
  sum L_apg_DR  if yes_is_lib != . & spending != 1
   local AL_ilbm_DR  = r(mean)
   local N_ilbm      = r(N)
  sum L_pct_D_pid if yes_is_lib != . & spending == 1
   local L_tlbm_Dpid = r(mean)
  sum L_pct_R_pid if yes_is_lib != . & spending == 1
   local L_tlbm_Rpid = r(mean)
  sum L_diff_DR     if yes_is_lib != . & spending == 1
   local L_tlbm_DR   = r(mean)
  sum L_apg_DR  if yes_is_lib != . & spending == 1
   local AL_tlbm_DR  = r(mean)
   local N_tlbm      = r(N)

  // Figure 2: Histogram
  twoway (histogram Y_alo_DR, bin(50) fraction xtitle("Absolute Log Odds Ratio", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)), title("Local Ballot Measures", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 2 4 6 8 10, labsize(vlarge)) ylabel(0 .05 .10, labsize(vlarge)) 
  graph export "`PATH_FIG'/hist_local_ballot_measures_lo.pdf", replace

  twoway (histogram Y_apg_DR, bin(50) fraction xtitle("Absolute Partisan Gap", size(huge)) ///
  ytitle("") lcolor(gs0) fcolor(gs8)) , title("Local Ballot Measures", size(vhuge)) ///
    plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
	xlabel(0 20 40 60 80 100, labsize(vlarge)) ylabel(0 .05 .10 .15, labsize(vlarge))
  graph export "`PATH_FIG'/fg2_hist_local_ballot_measures.pdf", replace

  // Figure 3 and Appendix C: by size, competitiveness etc
  gen logN = log(N)
  reg Y_apg_DR logN
   local r2_lbm = e(r2)

  lowess Y_apg_DR logN, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
  title("Local Ballot Measures", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Log Total Voters", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xlabel(5 10 15, labsize(large)) ylabel(0 20 40 60 80 100, labsize(large))
  graph export "`PATH_FIG'/local_ballot_measures_vs_size.pdf", replace

  gen margin = abs(Y_pct - 50)
  lowess Y_apg_DR margin, plotregion(style(none)) graphregion(fcolor(white) lcolor(white)) ///
  title("Local Ballot Measures", size(huge)) msize(.4) mcolor(gs8) ///
    xtitle("Margin of Victory", size(vlarge)) ytitle("Absolute Partisan Gap", size(vlarge)) ///
	xlabel(0 10 20 30 40 50, labsize(large)) ylabel(0 20 40 60 80 100, labsize(large))
  graph export "`PATH_FIG'/local_ballot_measures_vs_margin.pdf", replace


/* NUMBERS FOR THE PAPER */
quietly {
	capture log close
    log using "`PATH_NUM'/contestsN_lno.tex", replace text
    noisily display %5.0f `N_lno'
    log close
	
	capture log close
    log using "`PATH_NUM'/contestsN_lbm.tex", replace text
    noisily display %5.0f `N_lbm'
    log close
}

quietly {
	local PATH_NUM = "~/Downloads/numbers"
	preserve
	use tbA1_counties-demographics.dta, clear
	keep if county_name != ""
	levelsof state
     local n_states = r(r)
	
	local n_counties = _N
	 
    capture log close
    log using "`PATH_NUM'/statesN_all.tex", replace text
    noisily display %3.0f `n_states'
    log close
	
	capture log close
    log using "`PATH_NUM'/countiesN_all.tex", replace text
    noisily display %3.0f `n_counties'
    log close
    restore
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_1.tex", replace text
  noisily display %4.1f `U_spo_c'
  log close
}
quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_2.tex", replace text
  noisily display %4.1f `U_lpo_c'
  log close
}
quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_3.tex", replace text
  noisily display %4.1f `U_sno_c'
  log close
}
quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_4.tex", replace text
  noisily display %4.1f `U_lno_c'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_partisan_1.tex", replace text
  noisily display %4.0f 100 * `sum_sno_over40'/`sum_sno'
  log close
}
quietly {
  capture log close
  log using "`PATH_NUM'/no_partisan_2.tex", replace text
  noisily display %4.0f 100 * `sum_lno_over40'/`sum_lno'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_partisan_3.tex", replace text
  noisily display %4.0f `mean_AA_sno_x'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_partisan_4.tex", replace text
  noisily display %4.0f 100 * `sum_sno_over40_x'/`sum_sno_x'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_corr_sno.tex", replace text
  noisily display %5.2f `U_sno_comp_PScorr'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_corr_lno.tex", replace text
  noisily display %4.2f `U_lno_comp_PScorr'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_corr_sbm.tex", replace text
  noisily display %5.2f `U_sbm_comp_PScorr'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_rolloff_corr_lbm.tex", replace text
  noisily display %4.2f `U_lbm_comp_PScorr'
  log close
}

quietly {
  capture log close
  log using "`PATH_NUM'/no_apg_spo.tex", replace text
  noisily display %4.0f `AD_spo_DR'
  log close
}

quietly {
    capture log close
    log using "`PATH_NUM'/no_size_1.tex", replace text
    noisily display %4.2f `r2_lpo'
    log close
}

quietly {
    capture log close
    log using "`PATH_NUM'/no_size_2.tex", replace text
    noisily display %4.2f `r2_npo'
    log close
}

quietly {
    capture log close
    log using "`PATH_NUM'/no_size_3.tex", replace text
    noisily display %4.2f `r2_lbm'
    log close
}


// share over APG > 40
display 100 * `sum_sbm_over40'/`sum_sbm'
display 100 * `sum_lbm_over40'/`sum_lbm'



/* Full Tables */

/* Table 1: Voting Patterns by Partisanship */
quietly {
  capture log close
  log using "`PATH_TBL'/table_1.tex", replace text
  noisily display "\begin{table}[htpb] "
  noisily display "\centering "
  noisily display "\begin{threeparttable} "
  noisily display "\caption{\bf Voting Patterns by Partisanship} "
  noisily display "\label{table_1} "
  noisily display "\begin{tabular}{l c c c r} "
  noisily display "\toprule\toprule "
  noisily display "               &            &            & Absolute     & \#    \\ "
  noisily display "Contest Type   & $ D^D $    & $ D^R $    & Partisan Gap & Items \\ "
  noisily display "\midrule "
  noisily display "State Partisan Offices     & " %4.1f `D_spo_Dpid' " & " %4.1f `D_spo_Rpid' " & " %4.1f `AD_spo_DR' " & " %4.0f `N_spo' " \\ "
  noisily display "Local Partisan Offices     & " %4.1f `D_lpo_Dpid' " & " %4.1f `D_lpo_Rpid' " & " %4.1f `AD_lpo_DR' " & " %4.0f `N_lpo' " \\ "
  noisily display "\midrule "
  noisily display "State Nonpartisan Offices  & "                    " & "                    " & " %4.1f `AA_sno_DR' " & " %4.0f `N_sno' " \\ "
  noisily display "Local Nonpartisan Offices  & "                    " & "                    " & " %4.1f `AA_lno_DR' " & " %4.0f `N_lno' " \\ "
  noisily display "\midrule "
  noisily display "State Ballot Measures      & "                    " & "                    " & " %4.1f `AY_sbm_DR' " & " %4.0f `N_sbm' " \\ "
  noisily display "Local Ballot Measures      & "                    " & "                    " & " %4.1f `AY_lbm_DR' " & " %4.0f `N_lbm' " \\ "
  noisily display "\bottomrule "
  noisily display "\end{tabular} "
  noisily display "\begin{tablenotes} "
  noisily display "\footnotesize \item $ D^D$ is the average percentage of the two-party vote cast for the Democratic candidate by National Democrats, "
  noisily display "and $ D^R$ is the percentage of the two-party vote cast for the Democratic candidate by National Republicans. "
  noisily display "\end{tablenotes} "
  noisily display "\end{threeparttable} "
  noisily display "\end{table} "
  log close
}

/* Table 2: Roll-Off and Votes for Minor Candidates, in Percentages */
quietly {
  capture log close
  log using "`PATH_TBL'/table_2.tex", replace text
  noisily display "\begin{table}[htpb] "
  noisily display "\centering "
  noisily display "\begin{threeparttable} "
  noisily display "\caption{\bf Roll-Off and Votes for Minor Candidates, in Percentages} "
  noisily display "\label{table_2} "
  noisily display "\begin{tabular}{l c c r c r} "
  noisily display "\toprule\toprule "
  noisily display "               & \multicolumn{3}{c}{Contested Cases} ~~ & \multicolumn{2}{c}{Uncontested} \\ \cmidrule(lr){2-4} \cmidrule(lr){5-6}"
  noisily display "               & Roll-  & Minor  &                        & Roll-    &        \\ "
  noisily display "Contest Type   & Off   & Cands.  & Items ~                & Off     & Items  \\ "
  noisily display "\midrule "
  noisily display "State Partisan Offices     & " %4.1f `U_spo_c' " & " %4.1f `X_spo_c' " & " %4.0f `N_spo_c' "~~ &  " %4.1f `U_spo_u' " & " %4.0f `N_spo_u' " \\ "
  noisily display "Local Partisan Offices     & " %4.1f `U_lpo_c' " & " %4.1f `X_lpo_c' " & " %4.0f `N_lpo_c' "~~ &  " %4.1f `U_lpo_u' " & " %4.0f `N_lpo_u' " \\ "
  noisily display "\midrule "
  noisily display "State Nonpartisan Offices  & " %4.1f `U_sno_c' " & " %4.1f `X_sno_c' " & " %4.0f `N_sno_c' "~~ &  " %4.1f `U_sno_u' " & " %4.0f `N_sno_u' " \\ "
  noisily display "Local Nonpartisan Offices  & " %4.1f `U_lno_c' " & " %4.1f `X_lno_c' " & " %4.0f `N_lno_c' "~~ &  " %4.1f `U_lno_u' " & " %4.0f `N_lno_u' " \\ "
  noisily display "\midrule "
  noisily display "State Ballot Measures      & " %4.1f `U_sbm_c' " & "                 " & " %4.0f `N_sbm_c' "~~ &     &     \\ "
  noisily display "Local Ballot Measures      & " %4.1f `U_lbm_c' " & "                 " & " %4.0f `N_lbm_c' "~~ &     &     \\ "
  noisily display "\bottomrule "
  noisily display "\end{tabular}"
  noisily display "\end{threeparttable} "
  noisily display "\end{table} "
  log close
}


/* Table 4: Voting Patterns by Partisanship, Selected Local Offices */
quietly {
  capture log close
  log using "`PATH_TBL'/table_4.tex", replace text
  noisily display "\begin{table}[htpb] "
  noisily display "\centering "
  noisily display "\begin{threeparttable} "
  noisily display "\caption{\bf Voting Patterns by Partisanship, Selected Local Offices} "
  noisily display "\label{table_4} "
  noisily display "\begin{tabular}{l c c c r} "
  noisily display "\toprule\toprule "
  noisily display "               &            &            & Absolute     & \#    \\ "
  noisily display "Office         & $ D^D $    & $ D^R $    & Partisan Gap & Items \\ "
  noisily display "\midrule "
  noisily display "\multicolumn{5}{l}{\it Local Partisan Races} \\" 
  noisily display "County Legislature  &  " %4.1f `D_CO_LEG_Dpid'  " & " %4.1f `D_CO_LEG_Rpid'  " & " %4.1f `AD_CO_LEG_DR'  " & " %4.0f `N_CO_LEG'   " \\ "
  noisily display "Other County Office &  " %4.1f `D_CO_OTH_Dpid'  " & " %4.1f `D_CO_OTH_Rpid'  " & " %4.1f `AD_CO_OTH_DR'  " & " %4.0f `N_CO_OTH'   " \\ "
  noisily display "Municip Legislature &  " %4.1f `D_MUN_LEG_Dpid' " & " %4.1f `D_MUN_LEG_Rpid' " & " %4.1f `AD_MUN_LEG_DR' " & " %4.0f `N_MUN_LEG'  " \\ "
  noisily display "Mayor/Municip Exec  &  " %4.1f `D_MAYOR_Dpid'   " & " %4.1f `D_MAYOR_Rpid'   " & " %4.1f `AD_MAYOR_DR'   " & " %4.0f `N_MAYOR'    " \\ "
  noisily display "County Sheriff      &  " %4.1f `D_SHERIFF_Dpid' " & " %4.1f `D_SHERIFF_Rpid' " & " %4.1f `AD_SHERIFF_DR' " & " %4.0f `N_SHERIFF'  " \\ "
  noisily display "\midrule "
  noisily display "\multicolumn{5}{l}{\it Local Nonpartisan Races} \\" 
  noisily display "County Legislature  &  "                        " &  "                       " & " %4.1f `AA_CO_LEG_DR'  " & " %4.0f `N_CO_LEG_n'  " \\ "
  noisily display "Municip Legislature &  "                        " &  "                       " & " %4.1f `AA_MUN_LEG_DR' " & " %4.0f `N_MUN_LEG_n' " \\ "
  noisily display "Mayor/Municip Exec  &  "                        " &  "                       " & " %4.1f `AA_MAYOR_DR'   " & " %4.0f `N_MAYOR_n'   " \\ "
  noisily display "Local School Board  &  "                        " &  "                       " & " %4.1f `AA_LOC_BOE_DR' " & " %4.0f `N_LOC_BOE_n' " \\ "
  noisily display "\bottomrule "
  noisily display "\end{tabular}"
  noisily display "\begin{tablenotes} "
  noisily display "\footnotesize \item $ D^D$ is the average percentage of the two-party vote cast for the Democratic candidate by National Democrats, "
  noisily display "and $ D^R$ is the percentage of the two-party vote cast for the Democratic candidate by National Republicans. "
  noisily display "\end{tablenotes} "
  noisily display "\end{threeparttable} "
  noisily display "\end{table} "
  log close
}

/* Table 5: Voting Patterns by Partisanship, Ballot Measures by Type */
quietly {
  capture log close
  log using "`PATH_TBL'/table_5.tex", replace text
  noisily display "\begin{table}[htpb] "
  noisily display "\centering "
  noisily display "\begin{threeparttable} "
  noisily display "\caption{\bf Voting Patterns by Partisanship, Ballot Measures by Type} "
  noisily display "\label{table_5} "
  noisily display "\begin{tabular}{l c c c c r} "
  noisily display "\toprule\toprule "
  noisily display "               &            &            & Partisan & Absolute     & \#    \\ "
  noisily display "Contest Type   & $ L^D $    & $ L^R $    & Gap      & Partisan Gap & Items \\ "
  noisily display "\midrule "
  noisily display "State Ideological Measures & " %4.1f `L_isbm_Dpid' " & " %4.1f `L_isbm_Rpid' " & " %4.1f `L_isbm_DR' " & " %4.1f `AL_isbm_DR' " & " %4.0f `N_isbm' " \\ "
  noisily display "Local Ideological Measures & " %4.1f `L_ilbm_Dpid' " & " %4.1f `L_ilbm_Rpid' " & " %4.1f `L_ilbm_DR' " & " %4.1f `AL_ilbm_DR' " & " %4.0f `N_ilbm' " \\ "
  noisily display "State Tax/Spend Measures   & " %4.1f `L_tsbm_Dpid' " & " %4.1f `L_tsbm_Rpid' " & " %4.1f `L_tsbm_DR' " & " %4.1f `AL_tsbm_DR' " & " %4.0f `N_tsbm' " \\ "
  noisily display "Local Tax/Spend Measures   & " %4.1f `L_tlbm_Dpid' " & " %4.1f `L_tlbm_Rpid' " & " %4.1f `L_tlbm_DR' " & " %4.1f `AL_tlbm_DR' " & " %4.0f `N_tlbm' " \\ "
  noisily display "\bottomrule "
  noisily display "\end{tabular}"
  noisily display "\begin{tablenotes} "
  noisily display "\footnotesize \item $ L^D$ is the average percentage of the two-party vote cast for the Liberal alternative by National Democrats, "
  noisily display "and $ L^R$ is the percentage of the two-party vote cast for the Liberal alternative by National Republicans. "
  noisily display "\end{tablenotes} "
  noisily display "\end{threeparttable} "
  noisily display "\end{table} "
  log close
}

/* Table 6: Voting on Local Ballot Measures By Issue */
quietly {
  use wide_local_ballot_measures, clear
  
  gen Y_pct = 100 * Y_choice/(Y_choice + N_choice)
  gen competitive = 1 if (Y_pct > 5 & Y_pct < 95)
  keep if competitive == 1
  
  gen Y_pct_D_pid = 100 * Y_choice_D_pid/(Y_choice_D_pid + N_choice_D_pid)
  gen Y_pct_R_pid = 100 * Y_choice_R_pid/(Y_choice_R_pid + N_choice_R_pid)
  gen Y_diff_DR     = (Y_pct_D_pid - Y_pct_R_pid)
  gen Y_apg_DR  = abs(Y_pct_D_pid - Y_pct_R_pid)
  
  gen     L_pct       =       Y_pct       if yes_is_lib == 1
  replace L_pct       = 100 - Y_pct       if yes_is_lib == 0
  gen     L_pct_D_pid =       Y_pct_D_pid if yes_is_lib == 1
  replace L_pct_D_pid = 100 - Y_pct_D_pid if yes_is_lib == 0
  gen     L_pct_R_pid =       Y_pct_R_pid if yes_is_lib == 1
  replace L_pct_R_pid = 100 - Y_pct_R_pid if yes_is_lib == 0
  gen     L_diff_DR    =    (L_pct_D_pid - L_pct_R_pid) if yes_is_lib != .
  gen     L_apg_DR = abs(L_pct_D_pid - L_pct_R_pid) if yes_is_lib != .
  
  // loop through topic areas
  local j = 1
  foreach i in Education Libraries Fire Parks Police Roads Water Housing Healthcare Genl {
    sum L_pct       if spending == 1 & strpos(topic, "`i'") > 0
     local a`j' = r(mean)
    sum L_pct_D_pid if spending == 1 & strpos(topic, "`i'") > 0
     local d`j' = r(mean)
    sum L_pct_R_pid if spending == 1 & strpos(topic, "`i'") > 0
     local r`j' = r(mean)
    sum L_diff_DR   if spending == 1 & strpos(topic, "`i'") > 0
     local dr`j' = r(mean)
    sum L_apg_DR  if spending == 1 & strpos(topic, "`i'") > 0
     local dra`j' = r(mean)
     local n`j'   = r(N)
     local j = `j' + 1
  }
  local j = 11
  foreach i in Zoning Governance Liquor Term_Limits {
    sum Y_pct       if yes_is_lib == . & strpos(topic, "`i'") > 0
     local a`j' = r(mean)
    sum Y_pct_D_pid if yes_is_lib == . & strpos(topic, "`i'") > 0
     local d`j' = r(mean)
    sum Y_pct_R_pid if yes_is_lib == . & strpos(topic, "`i'") > 0
     local r`j' = r(mean)
    sum Y_apg_DR  if yes_is_lib == . & strpos(topic, "`i'") > 0
     local dra`j' = r(mean)
     local n`j'   = r(N)
     local j = `j' + 1
  }
  gen unclassified = 1 if yes_is_lib == . & spending != 1 & strpos(topic, "Zoning") == 0 & strpos(topic, "Governance") == 0 & strpos(topic, "Liquor") == 0 & strpos(topic, "Term_Limits") == 0
  sum Y_pct       if unclassified == 1
   local a15 = r(mean)
  sum Y_pct_D_pid if unclassified == 1
   local d15 = r(mean)
  sum Y_pct_R_pid if unclassified == 1
   local r15 = r(mean)
  sum Y_apg_DR  if unclassified == 1
   local dra15 = r(mean)
   local n15   = r(N)

  // to generate a number for the text
  gen byte x = 0
  foreach i in Libraries Fire Parks Police Roads Water {
    replace x = 1 if spending == 1 & strpos(topic, "`i'") > 0
  }

}

// spending == 1 & strpos(topic, "`i'") > 0
sum L_apg_DR if x == 1

quietly {
  capture log close
  log using "`PATH_TBL'/table_6.tex", replace text
  noisily display "\begin{table}[htpb] "
  noisily display "\centering "
  noisily display "\begin{threeparttable} "
  noisily display "\caption{\bf Voting on Local Ballot Measures By Issue} "
  noisily display "\label{table_6} "
  noisily display "\begin{tabular}{l c c c c c r} "
  noisily display "\toprule\toprule "
  noisily display "            &          &         &         & Partisan & Absolute     & \#    \\ "
  noisily display "Issue       & $ Y^A $  & $ Y^D $ & $ Y^R $ & Gap      & Partisan Gap & Items \\ "
  noisily display "\midrule "
  noisily display "\multicolumn{7}{l}{\it Voting for more taxes, spending, or bonds} \\ [.04in]"
  noisily display "~~Education           &  " %4.1f `a1'  " & " %4.1f `d1'  " & " %4.1f `r1'  " & " %4.1f `dr1'  " & " %4.1f `dra1'  " & " %3.0f `n1'  " \\ "
  noisily display "~~Libraries           &  " %4.1f `a2'  " & " %4.1f `d2'  " & " %4.1f `r2'  " & " %4.1f `dr2'  " & " %4.1f `dra2'  " & " %3.0f `n2'  " \\ "
  noisily display "~~Fire/EMS            &  " %4.1f `a3'  " & " %4.1f `d3'  " & " %4.1f `r3'  " & " %4.1f `dr3'  " & " %4.1f `dra3'  " & " %3.0f `n3'  " \\ "
  noisily display "~~Parks/Recreation ~~ &  " %4.1f `a4'  " & " %4.1f `d4'  " & " %4.1f `r4'  " & " %4.1f `dr4'  " & " %4.1f `dra4'  " & " %3.0f `n4'  " \\ "
  noisily display "~~Police              &  " %4.1f `a5'  " & " %4.1f `d5'  " & " %4.1f `r5'  " & " %4.1f `dr5'  " & " %4.1f `dra5'  " & " %3.0f `n5'  " \\ "
  noisily display "~~Roads               &  " %4.1f `a6'  " & " %4.1f `d6'  " & " %4.1f `r6'  " & " %4.1f `dr6'  " & " %4.1f `dra6'  " & " %3.0f `n6'  " \\ "
  noisily display "~~Water/Sanitation ~~ &  " %4.1f `a7'  " & " %4.1f `d7'  " & " %4.1f `r7'  " & " %4.1f `dr7'  " & " %4.1f `dra7'  " & " %3.0f `n7'  " \\ [.07in] "
  noisily display "~~Housing             &  " %4.1f `a8'  " & " %4.1f `d8'  " & " %4.1f `r8'  " & " %4.1f `dr8'  " & " %4.1f `dra8'  " & " %3.0f `n8'  " \\ "
  noisily display "~~Healthcare          &  " %4.1f `a9'  " & " %4.1f `d9'  " & " %4.1f `r9'  " & " %4.1f `dr9'  " & " %4.1f `dra9'  " & " %3.0f `n9'  " \\ [.07in] "
  noisily display "~~General/Multiple ~~ &  " %4.1f `a10' " & " %4.1f `d10' " & " %4.1f `r10' " & " %4.1f `dr10' " & " %4.1f `dra10' " & " %3.0f `n10' " \\ "
  noisily display "\midrule "
  noisily display "\multicolumn{7}{l}{\it Voting in favor, non-ideological, non-fiscal measures} \\ [.04in]"
  noisily display "~~Zoning/Land Use     &  " %4.1f `a11' " & " %4.1f `d11' " & " %4.1f `r11' " & "              " & " %4.1f `dra11' " & " %3.0f `n11' " \\ "
  noisily display "~~Governance          &  " %4.1f `a12' " & " %4.1f `d12' " & " %4.1f `r12' " & "              " & " %4.1f `dra12' " & " %3.0f `n12' " \\ "
  noisily display "~~Alcohol             &  " %4.1f `a13' " & " %4.1f `d13' " & " %4.1f `r13' " & "              " & " %4.1f `dra13' " & " %3.0f `n13' " \\ "
  noisily display "~~Term Limits         &  " %4.1f `a14' " & " %4.1f `d14' " & " %4.1f `r14' " & "              " & " %4.1f `dra14' " & " %3.0f `n14' " \\ "
  noisily display "~~Unclassified        &  " %4.1f `a15' " & " %4.1f `d15' " & " %4.1f `r15' " & "              " & " %4.1f `dra15' " & " %3.0f `n15' " \\ "
  noisily display "\bottomrule "
  noisily display "\end{tabular}"
  noisily display "\begin{tablenotes} "
  noisily display "\footnotesize \item $ Y^A$ is the average percentage of the two-party vote cast for the Yes alternative by all voters, "
  noisily display "$ Y^D$ is the average percentage of the two-party vote cast for the Yes alternative by National Democrats, "
  noisily display "and $ Y^R$ is the percentage of the two-party vote cast for the Yes alternative by National Republicans. "
  noisily display "\end{tablenotes} "
  noisily display "\end{threeparttable} "
  noisily display "\end{table} "
  log close
}


/*  Table B.4: Voting Patterns by Partisanship, Log Odds Measure */
quietly {
  capture log close
  log using "`PATH_TBL'/table_B4.tex", replace text
  noisily display "\begin{table}[htpb] "
  noisily display "\centering "
  noisily display "\begin{threeparttable} "
  noisily display "\caption{\bf Voting Patterns by Partisanship, Log Odds Measure} "
  noisily display "\label{table_A1} "
  noisily display "\begin{tabular}{l c r} "
  noisily display "\toprule\toprule "
  noisily display "               & Absolute & \#    \\ "
  noisily display "Contest Type   & Log Odds & Items \\ "
  noisily display "\midrule "
  noisily display "State Partisan Offices~~     & " %4.1f `ADL_spo_DR' " & " %4.0f `N_spo' " \\ "
  noisily display "Local Partisan Offices~~     & " %4.1f `ADL_lpo_DR' " & " %4.0f `N_lpo' " \\ "
  noisily display "\midrule "
  noisily display "State Nonpartisan Offices~~  & " %4.1f `AAL_sno_DR' " & " %4.0f `N_sno' " \\ "
  noisily display "Local Nonpartisan Offices~~  & " %4.1f `AAL_lno_DR' " & " %4.0f `N_lno' " \\ "
  noisily display "\midrule "
  noisily display "State Ballot Measures~~      & " %4.1f `AYL_sbm_DR' " & " %4.0f `N_sbm' " \\ "
  noisily display "Local Ballot Measures~~      & " %4.1f `AYL_lbm_DR' " & " %4.0f `N_lbm' " \\ "
  noisily display "\bottomrule "
  noisily display "\end{tabular}"
  noisily display "\end{threeparttable} "
  noisily display "\end{table} "
  log close
}


